package co.th.bjc.md.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import co.th.bjc.md.dao.LoginDao;
import co.th.bjc.md.model.Login;

public class LoginDaoImpl extends HibernateDaoSupport  implements LoginDao {

	@Override
	public Login findByUsername(String username) throws Exception {
		return (Login) getHibernateTemplate().get(Login.class, username);
	}

	@Override
	public Login verifyLogin(String username, String password) throws Exception {
		if (username == null || password == null){
			throw new Exception("Username or Password is null.");
		}
		
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		session.beginTransaction();
		
		String hql = "FROM login where username = :username AND password = :password";
		Query query = session.createQuery(hql);
		query.setParameter("username", username);
		query.setParameter("password", password);
		List<Login> list = query.list();
		
		return list.size()>0?list.get(0):null;
	}

}
